//
//  MPReplyToolBarView.m
//  kissfire
//
//  Created by Plum on 16/2/3.
//  Copyright © 2016年 manpaoTech. All rights reserved.
//

#import "MPReplyToolBarView.h"

//#import "CONST.h"


@interface MPReplyToolBarView ()<UIActionSheetDelegate,UINavigationControllerDelegate, UIImagePickerControllerDelegate>


@end
@implementation MPReplyToolBarView
{
    UILabel *countLabel;
    BOOL _isHidenName;
}
#pragma mark -

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark -
- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self){
        
        
        [self initView];
        self.limitCount = 300;
        _isHidenName = NO;
      
        
    }
    return self;
}
#pragma mark - 
- (void)initView
{
    
    self.backgroundColor = [UIColor groupTableViewBackgroundColor];
    CGFloat left = 15;
    
    CGFloat width = kScreenWidth - left*2;

    CGFloat top = 20;

    CGFloat bottomH = 50;
    
    CGFloat bgH = 75;

    UIView *bgVi = [[UIView alloc]initWithFrame:CGRectMake(left, top, width, bgH)];
    bgVi.backgroundColor = [UIColor whiteColor];
    bgVi.layer.borderColor = [UIColor colorWithWhite:226/255.0 alpha:1].CGColor;
    bgVi.layer.borderWidth = 1;
    [self addSubview:bgVi];
    self.bgView = bgVi;
    
    [self initBgView];

    CGFloat bottomBtnW = 60;
    
    CGFloat btnImgW = 22;
    CGFloat btnImL = (bottomBtnW - btnImgW)/2;
    CGFloat btnImT = (bottomH - btnImgW)/2;

    CGFloat bottomT = _bgView.bottom;
    for (int i = 0; i < 2; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:btn];
     
        if (i == 0) {
            self.addImgBtn = btn;
            
            [btn setImageEdgeInsets:UIEdgeInsetsMake(btnImT, 0, btnImT, btnImL*2)];
            
            
            btn.frame = CGRectMake(left, bottomT, bottomBtnW, bottomH);
    
            [btn setImage:[UIImage imageNamed:@"传图1"] forState:0];
            [btn setImage:[UIImage imageNamed:@"smile"] forState:UIControlStateSelected];
            [btn setTitle:@"匿名" forState:UIControlStateNormal];
            btn.titleLabel.font = [UIFont systemFontOfSize:14];
            [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(hideName:) forControlEvents:UIControlEventTouchUpInside];
            
            
        
           
        }
        if (i == 1) {
            self.attendBtn = btn;
            
            CGFloat btnW = 55;
            btn.frame = CGRectMake(kScreenWidth - btnW -left, bottomT+btnImT-2, btnW, btnImgW+4);
            
            btn.layer.borderColor = [UIColor colorWithWhite:226/255.0 alpha:1].CGColor;
            btn.layer.cornerRadius = 2;
            btn.layer.masksToBounds = YES;

            btn.titleLabel.font = [UIFont systemFontOfSize:15];
         
            [btn setTitle:@"发表" forState:0];
            [btn setTitleColor:[UIColor colorWithWhite:0.811 alpha:1.000] forState:UIControlStateDisabled];
            [btn setTitleColor:[UIColor whiteColor] forState:0];
            [btn addTarget:self action:@selector(done) forControlEvents:UIControlEventTouchUpInside];
            UILabel *label =[UILabel new];
            countLabel  = label;
            label.frame = CGRectMake(kScreenWidth - btnW -left -108, bottomT+btnImT-2 + 5, 100, 24);
            label.font = [UIFont systemFontOfSize:14];
           
            label.text = @"123/500";
            label.textAlignment = NSTextAlignmentRight;
       
            [self addSubview:label];
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateCountLabel:) name:UITextViewTextDidChangeNotification object:nil];
            countLabel.text = [NSString stringWithFormat:@"%d/%ld",0,(long)self.limitCount];
          
            [self PostDisEnable];

        }
    }
  
    
    self.height = bottomT +bottomH +15;

}

- (void)done{
    
   
   
    [UIView animateWithDuration:.35f animations:^{
        self.left = kScreenWidth;
    } completion:^(BOOL finished) {
        if (self.doneBlock) {
            self.doneBlock(self.textView.text,_isHidenName);
        }
        [self.selfVc dismissViewControllerAnimated:NO completion:nil];
    }];
    

}

- (void)hideName:(UIButton *)btn{
    btn.selected = !btn.selected;
    _isHidenName = !_isHidenName;

}

- (void)initBgView
{
    
    CGFloat bgH = _bgView.height;
    CGFloat bgW = _bgView.width;
    
    CGFloat imgW = bgH;
    
    CGFloat sep = 5;
    UIImageView *headImgVi = [[UIImageView alloc]initWithFrame:CGRectMake(bgW - imgW +sep, sep, imgW-sep*2, imgW-sep*2)];
    headImgVi.contentMode = UIViewContentModeScaleAspectFill;
    headImgVi.layer.masksToBounds = YES;
    headImgVi.userInteractionEnabled = YES;
    [_bgView addSubview:headImgVi];
    self.imgView = headImgVi;
    
    CGFloat deleteBtnW = 40;
    CGFloat btnImgW = 17;
    CGFloat btnImL = (deleteBtnW - btnImgW)/2;
    
    UIButton *left = [UIButton buttonWithType:UIButtonTypeCustom];
    left.frame = CGRectMake(headImgVi.width-deleteBtnW, 0, deleteBtnW, deleteBtnW);
    left.userInteractionEnabled = YES;
    [left addTarget:self action:@selector(deleteImage) forControlEvents:UIControlEventTouchUpInside];
    [left setImage:[UIImage imageNamed:@"删除图片"] forState:0];
    [left setImageEdgeInsets:UIEdgeInsetsMake(0, btnImL*2, btnImL*2, 0)];
    [headImgVi addSubview:left];
    
    PlaceholderTextView *contentView = [[PlaceholderTextView alloc]initWithFrame:CGRectMake(0, 0, bgW,bgH)];
    contentView.font = [UIFont  systemFontOfSize:15];
    contentView.placeholder = @"请输入您的评论内容";
    contentView.placeholderColor = [UIColor lightGrayColor];
    contentView.placeholderFont = [UIFont  systemFontOfSize:15];
    contentView.backgroundColor = [UIColor whiteColor];
    contentView.delegate = self;
    //contentView.returnKeyType = UIReturnKeyDone;
    [_bgView addSubview:contentView];
    self.textView = contentView;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(deleteImage)];
    [headImgVi addGestureRecognizer:tap];


}



#pragma mark -
- (void)deleteImage
{
//    chooseImg = nil;
//    [self layerImgView];
    
}
//- (void)addImgBtnClicked
//{
//    
//    [self tapHeagImg];
//    
//}


#pragma mark -
- (void)layerImgView
{
    
    CGFloat bgH = _bgView.height;
    CGFloat bgW = _bgView.width;
    CGFloat imgW = bgH;
    
    _imgView.image = chooseImg;
    if (chooseImg) {
       _textView.width = bgW - imgW;
        
    }else{
       _textView.width = bgW;

    }
    

}

#pragma mark- 监听文本改变

- (void)updateCountLabel:(NSNotification *)not{
    
    //    NSInteger count = [text length];
    //    self.countLabel.text = [NSString stringWithFormat:@"%d/%d",count,self.limitCount];
    UITextView *textView = not.object;
    
    
    
    
    NSString *lang = [[UITextInputMode currentInputMode] primaryLanguage]; // 键盘输入模式
    NSString *toBeString = textView.text;
    if ([lang isEqualToString:@"zh-Hans"]) { // 简体中文输入，包括简体拼音，健体五笔，简体手写
        UITextRange *selectedRange = [textView markedTextRange];
        //获取高亮部分
        UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
        // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
        if (!position) {
            if (toBeString.length > self.limitCount) {
                textView.text = [toBeString substringToIndex:self.limitCount];
            }
        }
        // 有高亮选择的字符串，则暂不对文字进行统计和限制
        else{
            
        }
    }
    // 中文输入法以外的直接对其统计限制即可，不考虑其他语种情况
    else{
        if (toBeString.length > self.limitCount) {
            textView.text = [toBeString substringToIndex:self.limitCount];
        }
    }
    if (textView.text.length >0) {
        [self PostEnable];
    }else{
        [self PostDisEnable];
    }
    countLabel.text = [NSString stringWithFormat:@"%d/%d",textView.text.length,self.limitCount];
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    NSString *txt = textView.text;
    if (txt.length == 0 && [text isEqualToString:@"\n"]) {
        return NO;
    }
    return YES;
}

//是否为空格
- (BOOL)isEmpty:(NSString *) str {
    
    if (!str) {
        return true;
    } else {
        //A character set containing only the whitespace characters space (U+0020) and tab (U+0009) and the newline and nextline characters (U+000A–U+000D, U+0085).
        NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
        
        //Returns a new string made by removing from both ends of the receiver characters contained in a given character set.
        NSString *trimedString = [str stringByTrimmingCharactersInSet:set];
        
        if ([trimedString length] == 0) {
            return true;
        } else {
            return false;
        }
    }
}



- (void)PostEnable
{
    _attendBtn.enabled = YES;
    
    _attendBtn.layer.borderWidth = 0;
    _attendBtn.backgroundColor = [UIColor colorWithRed:237.0/255.0 green:76.0/255.0 blue:42.0/255.0 alpha:1];
    
}
- (void)PostDisEnable
{
    
    _attendBtn.enabled = NO;
    _attendBtn.layer.borderWidth = 1;
    _attendBtn.backgroundColor = [UIColor clearColor];
    
}


#pragma mark-- 更换图片

//-(void)tapHeagImg{
//    
//    NSUInteger sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
// 
//    sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
//    
//    // 跳转到相机或相册页面
//    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
//    
//    imagePickerController.delegate = self;
//    //imagePickerController.allowsEditing = YES;
//    imagePickerController.sourceType = sourceType;
//    [_selfVc presentViewController:imagePickerController animated:YES completion:^{}];
//}


//#pragma mark - image picker delegte

//- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
//{
//    [_selfVc dismissViewControllerAnimated:YES completion:Nil];
//}
//
//- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
//{
//    
//    UIImage *pickImage = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
//    chooseImg = pickImage;
//    [self layerImgView];
//    [picker dismissViewControllerAnimated:YES completion:^{
//    }];
//    
//}


@end
